package com.scnu.recruitment.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.scnu.common.domain.po.User;
import com.scnu.common.utils.UserContext;
import com.scnu.recruitment.domain.po.ExpOrder;
import com.scnu.recruitment.enums.PayStatus;
import com.scnu.recruitment.mapper.ExpOrderMapper;
import com.scnu.recruitment.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scnu.recruitment.utils.WechatPay;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 快递代拿表 服务实现类
 * </p>
 *
 * @author startech
 * @since 2025-10-21
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<ExpOrderMapper, ExpOrder> implements IOrderService {

    private final WechatPay wechatPay;


    @Override
    public String addOrder(ExpOrder expOrder) {

        User user = UserContext.getUser();
        expOrder.setUserId(user.getUserId());
        expOrder.setUserNumberCode(user.getNumberCode());
        expOrder.setOrderTime(LocalDateTime.now());
        expOrder.setPayStatus(PayStatus.UNPAYED);
        expOrder.setOrderNumber(IdUtil.getSnowflakeNextId());
        expOrder.setOrderTime(LocalDateTime.now());
        expOrder.setFirstFee(null);
        expOrder.setFinalFee(null);

        String prepay_id = wechatPay.prepay(expOrder);


        expOrder.setPrepayId(prepay_id);


        save(expOrder);
        return prepay_id;


    }
}
